package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.pojo.User;
import com.example.demo.service.Impl.ConnectionServiceImpl;
import com.example.demo.service.Impl.UserServiceImpl;
import com.example.demo.util.Tools;
import com.example.demo.util.Unite;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


@RestController
public class UserController {

    @Autowired
    UserServiceImpl userService;
    @Autowired
    HttpServletRequest request;

    @Autowired
    ConnectionServiceImpl connectionService;

    @GetMapping("/UserList")
    public Unite getList(@RequestParam int index){
        int a= Tools.getSessionValue(request,"user");
        User user=userService.getUserById(a);
        Unite unite;
        List<User> list;
        try {
           list=userService.getUserBySxOri(index,user.getSexualOrien(),user.getGender());
        }catch (Exception e){
            return new Unite(null,false);
        }
        list.remove(user);
        return new Unite(list,true);
    }
    @GetMapping("/getDurations")
    public Unite getDurations(@RequestParam int user_id){
        List<Integer> list=new ArrayList<>();
        list=connectionService.getDurations(user_id);
        return new Unite(list,true);
    }
    @GetMapping("/getScores")
    public Unite getScores(@RequestParam int user_id){
        List<Integer> list=new ArrayList<>();
        list=connectionService.getScores(user_id);
        return new Unite(list,true);
    }
    @GetMapping("/getUser")
    public Unite getUser(@RequestParam Integer user_id){
        int a= Tools.getSessionValue(request,"user");
        if(user_id==null){
            user_id=a;
        }
        User user=userService.getUserById(user_id);
        return new Unite(user,true);
    }
    @PostMapping("/upload")
    public Unite upload(@RequestParam("file") MultipartFile fileUpload){
        int a= Tools.getSessionValue(request,"user");
        String fileName = fileUpload.getOriginalFilename().split("\\.")[0]+a+".jpg";
        String filePath = "E:\\java-ideas\\demo\\src\\main\\resources\\static\\img\\";
        UpdateWrapper<User> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("user_id",a).set("hp_url","http://localhost:8080/img/"+fileName);
        userService.update(updateWrapper);
        try {
            //将图片保存到static文件夹里
            fileUpload.transferTo(new File(filePath+fileName));
            //返回提示信息
            System.out.println(1);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(2);
        }
        return new Unite("http://localhost:8080/img/"+fileName,false);
    }
    @GetMapping("/updateUserInfo")
    public Unite updateUserInfo(@RequestParam String type,@RequestParam String value){
        int a= Tools.getSessionValue(request,"user");
        UpdateWrapper<User> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("user_id",a).set(type,value);
        userService.update(updateWrapper);
        return new Unite(null,true);
    }
    @GetMapping("/getUserState")
    public Unite getUserState(@RequestParam int id){
        String state=userService.updateState(id);
        if(state.equals("off")){
            return new Unite(null,true);
        }else{
            return new Unite(null,false);
        }
    }
    @GetMapping("/getUsers")
    public Unite getUsers(){
        List<User> list= userService.list();
        return new Unite(list,true);
    }
    @GetMapping("/updateTime")
    public Unite updateTime(@RequestParam Timestamp time, @RequestParam int id){
        UpdateWrapper<User> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("user_id",id);
        updateWrapper.set("lastTime", time);
        userService.update(updateWrapper);
        return null;
    }
}
